package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.SysProductsDao;
import com.gusha.model.InfoContent;
import com.gusha.model.SysClassify;
import com.gusha.model.SysProducts;
import com.gusha.service.SysProductsService;
import com.gusha.utils.Constants;
import com.gusha.utils.FileUtils;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/products")
public class SysProductsController {
	
	@Autowired
	private SysProductsDao sysProductsDao;
	@Autowired
	private SysProductsService sysProductsService;

    @RequestMapping("list")
    @SysOperateLog(operate = "查看产品列表")
	public String list(Model model, HttpServletRequest request) {
    	 String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
    	 String productsName = WebValueUtil.getWebValue(request, "productsName");
    	 String productsType = WebValueUtil.getWebValue(request, "productsType");
         int pageNo = 0;
         if (StringUtils.isNotBlank(pageIndex)) {
             pageNo = Integer.parseInt(pageIndex);
         }
         SysProducts products=new SysProducts();
         if (StringUtils.isNotBlank(productsName)) {
            products.setProductsName(productsName);
         }
         if (StringUtils.isNotBlank(productsType)) {
             products.setProductsType(new BigInteger(productsType));
          }
         
         Sort sort = new Sort(Sort.Direction.DESC, "addTime");
         Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
         Page<SysProducts> page = sysProductsService.findAll(pageable, products);
         
         model.addAttribute("page", page);
         
		return "/admin/sysProducts/list";
		
	}
    
    // 进入添加页面
    @RequestMapping("toAdd")
    @SysOperateLog(operate = "进入项目添加页面")
    public String toAdd(Model model) {
 
        return "/admin/sysProducts/form";
    }
	
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入资讯编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");

        if (StringUtils.isNotBlank(id)) {
            if(sysProductsDao.existsById(new BigInteger(id))) {
                SysProducts products = sysProductsDao.getOne(new BigInteger(id));
                model.addAttribute("obj", products);
            }
        }  
        return "/admin/sysProducts/form";
    }
    
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存资讯信息")
    public JsonResult save(SysProducts products,@RequestParam(name="file1") MultipartFile [] files,MultipartHttpServletRequest request) {
        JsonResult result = new JsonResult(false,"保存失败");
    	String n="";//将所有图片拼接到一个字符串里
    	System.out.println(files.length+"====filelength");
		for (MultipartFile file : files) {
			String req= file.getOriginalFilename();
			//     String newFileName = FileUtils.getFileName(file.getOriginalFilename());
			String imagePath = Constants.getFilePath("info")+req;
			String localPath = Constants.IMAGE_LOCAL_PATH+Constants.getFilePath("info");
			FileUtils.upload(file, localPath, req);
			n=n+"#"+imagePath;
			
		}
		products.setProductsImg(n);
        sysProductsDao.saveAndFlush(products);
        result.setFlag(true);
        result.setMsg("保存成功");
        return result;
    }
    
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除资讯信息")
    public JsonResult delete(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (sysProductsDao.existsById(id)) {
            SysProducts products = sysProductsDao.getOne(id);
            products.setIsDelete(new BigInteger("1"));
            sysProductsDao.saveAndFlush(products);
            result.setFlag(true);
            result.setMsg("删除成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }
    
    @RequestMapping("recovery")
    @ResponseBody
    @SysOperateLog(operate = "删除资讯信息")
    public JsonResult recovery(BigInteger id) {
        JsonResult result = new JsonResult(false,"删除失败");
        if (sysProductsDao.existsById(id)) {
            SysProducts products = sysProductsDao.getOne(id);
            products.setIsDelete(new BigInteger("0"));
            sysProductsDao.saveAndFlush(products);
            result.setFlag(true);
            result.setMsg("恢复成功");
        }else{
            result.setFlag(false);
            result.setMsg("该对象信息不存在");
            return result;
        }
        return result;
    }
}
